$(document).ready(function() {
	var orderFlowModel = function(orderModel) {
		var self = this;
		self.makingEnabled = ko.observable(true);
		self.outEnabled = ko.observable(true);
		self.finishedEnabled = ko.observable(true);
		self.ensureEnabled = ko.observable(true);
		var setCheckEnabled = function(enabled) {
			self.makingEnabled(enabled);
			self.outEnabled(enabled);
			self.finishedEnabled(enabled);
			self.ensureEnabled(enabled);
		}
		
		self.order = ko.mapping.fromJS(orderModel);
		self.orderDate = ko.computed(function() {
			if (self.order.time() == '') {
				return '';
			} else {
				return new Date(self.order.time()).format('yyyy-MM-dd      hh:mm:ss');
			}
		}, this);

		var myStatuses = ['making', 'out', 'finished'];
		var getStatuses = function(index) {
			if (index >= 0 && index <= 3) {
				var result = [];
				for(var i = 0; i < index; i++) {
					result.push(myStatuses[i]);
				}
				return result;
			}
		};

		self.myStatus = ko.observableArray([]);

		ko.computed(function() {
			if (self.order && self.order.orderStatus() != null) {
				self.myStatus(getStatuses(self.order.orderStatus()));
			}
		}, this);
		
		self.clickMaking = function() {
			setTimeout(function() {
				self.order.orderStatus(1);
			},1);
		};
		self.clickOut = function() {
			setTimeout(function() {
				self.order.orderStatus(2);
			},1);
		};
		self.clickFinished = function() {
			setTimeout(function() {
				self.order.orderStatus(3);
			},1);
		};
		
		//计算所有菜式总价的绑定
		self.total = ko.computed(function() {
			var total = 0;
			var totalOrderDishes = self.order.orderDishes();
			var orderDishesLength = totalOrderDishes.length;
			for (var i = 0; i < orderDishesLength;i++) {
				var orderDish = totalOrderDishes[i];
				if (orderDish.dishEnabled()) {
					var sum = orderDish.num() * orderDish.price();
					sum = roundNumber(sum, 2);
					orderDish.dishSum(sum);
					total = total + parseFloat(orderDish.dishSum());
				}
			}
			return roundNumber(total, 2);
		}, self);
		
		self.orderZhuangtai = ko.computed(function() {
			if (self.order && self.order.id() > 0) {
				return '正在处理订单';
			} else {
				return '暂时没有需要处理的订单';
			}
		}, self);

		self.getOrderById = function() {
			$.getJSON('flow/order/' + self.orderId(), function(response) {
				var order;
				if (response.order) {
					order = response.order;
				} else {
					order = blankOrder;
				}
				ko.mapping.fromJS(order, self.order);
				console.log(self.order.orderStatus());
				if (self.order.orderStatus() == '3') {
					//console.log(self.order.orderStatus());
					setCheckEnabled(false);
				} else {
					setCheckEnabled(true);
				}
			});
		};

		self.ensureStatus = function() {
			var status = self.order.orderStatus();
			var orderId = self.order.id();
			if (parseInt(status) >= 0 && parseInt(orderId) >= 0) {
				$.getJSON('flow/order/ensure/' + orderId + '/' + status + '?_method=PUT', function(response) {
					if (response.success == true) {
						alert('修改状态成功');
					} else {
						alert('修改状态失败,请重新操作');
					}
					ko.mapping.fromJS(blankOrder, self.order);
				});
			}
		};

		self.orderDraw = function() {
			var orderId = self.order.id();
			if (parseInt(orderId) >= 0) {
				$.getJSON('flow/ordre/draw/' + orderId + '?_method=PUT', function(response) {
					if (response.success == true) {
						alert('抽回成功');
						ko.mapping.fromJS(blankOrder, self.order);
					} else {
						alert('抽回失败,请重新操作');
						ko.mapping.fromJS(blankOrder, self.order);
					}
				});
			}
		};
		
		self.orderCancled = function() {
			var orderId = self.order.id();
			if (parseInt(orderId) >= 0) {
				$.getJSON('cancled/' + orderId + '?_method=PUT', function(response) {
					if (response.success == true) {
						alert('作废成功');
						ko.mapping.fromJS(blankOrder, self.order);
					} else {
						alert('作废失败,请重新操作');
						ko.mapping.fromJS(blankOrder, self.order);
					}
				});
			}
		};
		
		self.orderId = ko.observable('');
	};

	var flowModel = new orderFlowModel(blankOrder);
	ko.applyBindings(flowModel, document.getElementById('tabs-workflow'));
});